package com.whisperly.echotalk.starryb.utils;

import static android.content.ContentValues.TAG;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.attribution.AppsFlyerRequestListener;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.hjq.toast.Toaster;
import com.whisperly.echotalk.bllmodel.UserWithChat;
import com.whisperly.echotalk.starryb.R;
import com.whisperly.echotalk.starryb.netWorkUtils.NetworkUtils;

import java.util.Locale;
import java.util.Map;

import com.appsflyer.AppsFlyerLib;

public class Init extends Application {

    private static Context context;
    private static Init instance;
    private static UserWithChat user;
    private static String shareUrl = "";
    private static final String LOG_TAG = "AppsFlyer";
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        Toaster.init(this);
        instance = this;
        // 从持久化存储（如SharedPreferences）加载保存的语言设置
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String languageCode = prefs.getString("language", "en");
        // 设置语言
        updateResources(this, languageCode);
        AppsFlyerLib.getInstance().init("wh6NDe652Zdfa5zyTJEmDX", new AppsFlyerConversionListener() {
            @Override
            public void onConversionDataSuccess(Map<String, Object> map) {
                Log.d(LOG_TAG, "Launch sent successfully, got 200 response code from server");
            }

            @Override
            public void onConversionDataFail(String s) {

            }

            @Override
            public void onAppOpenAttribution(Map<String, String> map) {

            }

            @Override
            public void onAttributionFailure(String s) {
                Log.d(LOG_TAG, "Launch failed to be sent:\n" +
                        "Error description: " + s);
            }
        }, this);
        AppsFlyerLib.getInstance().start(this);

        new Thread(
                () -> {
                    // Initialize the Google Mobile Ads SDK on a background thread.
                    MobileAds.initialize(this, initializationStatus -> {});
                })
                .start();
    }
    public void updateResources(Context context, String languageCode) {
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.createConfigurationContext(config);

    }

    public static Context getContext() {
        return context;
    }

    public static void setContext(Context context) {
        Init.context = context;
    }

    public static Init getInstance() {
        return instance;
    }

    public static void setInstance(Init instance) {
        Init.instance = instance;
    }

    public static UserWithChat getUser() {
        return user;
    }

    public static void setUser(UserWithChat user) {
        Init.user = user;
    }

    public static String getShareUrl() {
        return shareUrl;
    }

    public static void setShareUrl(String shareUrl) {
        Init.shareUrl = shareUrl;
    }
}
